#!/usr/bin/python
# -*- coding: UTF-8 -*-

import base64
import json
import os
import requests

# 具体申请接口请查看http://ai.baidu.com/docs#/Begin/top
client_id ='2X7C7Q0jbQQuGsURUQIlTo3H'                   # API Key
client_secret ='qnmPylZ7HjVjobcfRtCYrQwzznVmxeuS'       # Secret Key

file_address = 'E:\\Users\\10178\\Desktop\\test\\01.wav'
f = open(file_address, 'rb')   
speech = base64.b64encode(f.read()).decode('utf8')

get_token_url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials'+'&client_id='+client_id+'&client_secret='+client_secret
# 鉴权认证机制获取 Access Token
token = json.loads(requests.post(get_token_url).text)["access_token"]

lens = os.path.getsize(file_address)
headers = { 'Content-Type' : 'application/json'} 
url = "https://vop.baidu.com/server_api"
data={
    "format":"wav",
    "rate":"16000",
    "dev_pid":"1536",
    "cuid":"wate_play",
    "channel":1,
    "speech":speech,
    "len":lens,
    "token":token,
}

res = requests.post(url,json.dumps(data),headers)
result = json.loads(res.text)
print(result)